package com.csq.search.service.impl;

import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csq.common.pojo.SearchResultData;
import com.csq.manager.pojo.Parttime;
import com.csq.manager.service.mapper.PartTimeDataMapper;
import com.csq.search.dao.SearchDao;
import com.csq.search.service.SearchParttimeService;

@Service
public class SearchParttimeServiceImpl implements SearchParttimeService {

	@Autowired
	private PartTimeDataMapper dataMapper;
	@Autowired
	private SolrServer solrServer;
	
	@Autowired
	private SearchDao searchDao;

	@Override
	public boolean importAllParttimeDataIntoSolr() throws SolrServerException,
			IOException {

		List<Parttime> partTimeList = dataMapper.getPartTimeList();

		for (Parttime parttime : partTimeList) {
			SolrInputDocument document = new SolrInputDocument();

			document.addField("", parttime.getParttimeId());
			document.addField("", parttime.getBusinessTime());
			document.addField("", parttime.getParttimeName());
			document.addField("", parttime.getWage());
			solrServer.add(document);
		}

		solrServer.commit();
		return true;

	}

	@Override
	public SearchResultData<Parttime> searchParttime(String keys, Integer page, Integer rows) throws SolrServerException {
		
		SolrQuery solrQuery = new SolrQuery();
		
		solrQuery.setStart(page-1);
		
		solrQuery.setRows(rows);
		
		// 搜索域
		solrQuery.set("df", "item_keywords");
//		solrQuery.setFields("item_keywords");
		
		solrQuery.setHighlight(true);
		
		solrQuery.addHighlightField("item_title");
		
		
//		solrQuery.setHighlightSimplePre(f)
//		solrQuery.setHighlightSimplePost(f)
		
		SearchResultData<Parttime> searchParttime = searchDao.searchParttime(solrQuery);
		
		searchParttime.setCurPage(page);
		
		long resultCount = searchParttime.getRecordCount();
		
		int pageCount = (int) (resultCount / rows);
		
		if(resultCount % rows != 0){
			pageCount ++;
		}
		
		searchParttime.setPageCount(pageCount);
		
		return searchParttime;
	}



}
